import { canvasHolder } from "./CanvasHolder";
import store from "@/store";
import {
  commonurl,
  defaultSkyBoxResUrl,
  userName,
  passWord,
  mainWndName,
} from "./config";

// 限制引擎使用资源，参数为郭金玺推荐
export function setupEngine() {
  if (BlackHole3D) {
    BlackHole3D.Common.setMaxResMemMB(5500);
    BlackHole3D.Common.setExpectMaxInstMemMB(4500);
    BlackHole3D.Common.setExpectMaxInstDrawFaceNum(20000000);
    BlackHole3D.Common.setPageLoadLev(2);
  }
}

//初始化引擎系统
export function initialSDKModule() {
  if (typeof window.BlackHole3D?.RealBIMWeb !== "undefined") {
    return;
  }
  if (typeof window.CreateBlackHoleWebSDK === "undefined") {
    return;
  }
  let canvasDom = canvasHolder.get();
  if (!canvasDom) {
    canvasDom = canvasHolder.create();
    document.body.appendChild(canvasDom);
  }
  //初始化引擎系统
  initRESystem(canvasDom);
  //系统初始化完成
  document.addEventListener("RESystemReady", (_) => {
    store.dispatch("setStoreState", { RESystemReady: true });
    console.log("RESystemReady Success ==> 引擎系统初始化完成");
    //初始化场景 && 加载公共资源
    initialEngine();
  });

  //系统引擎创建完成【公共资源加载完成】
  document.addEventListener("RESystemEngineCreated", (e) => {
    console.log("当前 WebSDK 运行版本", BlackHole3D.getVersion());
    console.log(
      "RESystemRenderReady Success ==> 系统引擎创建完成【公共资源加载完成】"
    );
    let isSuccess = e.detail.succeed;
    if (isSuccess) {
      console.log("场景初始化 --> 成功！！！");
      //当前可以执行加载模型相关操作
      // 设置全局渲染性能控制参数
      setupEngine();
      store.dispatch("setStoreState", { RESystemRenderReady: true });
    } else {
      store.dispatch("setStoreState", { RESystemRenderReady: false });
      console.log("场景初始化 --> 失败！！！");
    }
  });
}

//step1 初始化引擎系统
export function initRESystem(canvasDom = null) {
  if (!canvasDom) {
    console.warn("[function initRESystem()] ==> canvas does not exist");
    return;
  }
  window.BlackHole3D = window.BlackHole3D ? window.BlackHole3D : {};
  window.BlackHole3D["canvas"] = canvasDom;
  if (typeof window.BlackHole3D.RealBIMWeb === "undefined") {
    window.BlackHole3D = window.CreateBlackHoleWebSDK(window.BlackHole3D);
  }
}

//初始化场景 && 加载公共资源
export function initialEngine(width = 100, height = 100) {
  BlackHole3D.m_re_em_window_width = width;
  BlackHole3D.m_re_em_window_height = height;
  let sysInfo = new BlackHole3D.RESysInfo();
  sysInfo.workerjsPath = "./blackhole/RealBIMWeb_Worker.js";
  sysInfo.renderWidth = width;
  sysInfo.renderHieght = height;
  sysInfo.commonUrl = commonurl;
  sysInfo.userName = userName;
  sysInfo.passWord = passWord;
  sysInfo.mainWndName = mainWndName;
  BlackHole3D.initEngineSys(sysInfo); //初始化引擎
  BlackHole3D.Common.setUseWebCache(false); //是否允许使用浏览器缓存
  sessionStorage.setItem("engineLoaded", "true");
}

export function restEngine() {
  if (!window.BlackHole3D) {
    return;
  }
  let dom = document.getElementById("app-for-graphic");
  BlackHole3D.m_re_em_window_width = dom.clientWidth;
  BlackHole3D.m_re_em_window_height = dom.clientHeight;
  BlackHole3D.Graphics.setGeoCoordVisible(true);
  BlackHole3D.Graphics.setViewCubeVisible(true);
  BlackHole3D.Graphics.setSysUIPanelVisible(true);
  BlackHole3D.Graphics.setSysUIColorStyle(false);
  BlackHole3D.Graphics.setSysPanelUIDockArea(0);
  BlackHole3D.Camera.resetCamLocate();
  BlackHole3D.Camera.resetCamBound();
}

export function pageDestroy() {
  //移除bim相关时间监听
  document.removeEventListener("REDataSetLoadFinish", () => {});
  window.removeEventListener("resize", () => {});
  document.removeEventListener("RESystemSelShpElement", () => {});
  document.removeEventListener("RESystemSelElement", () => {});
  document.removeEventListener("RECameraMove", () => {});
  canvasHolder.moveCanvasToBody();
  if (!window.BlackHole3D) {
    return;
  }
  BlackHole3D.Graphics.setGeoCoordVisible(false);
  BlackHole3D.Graphics.setViewCubeVisible(false);
  BlackHole3D.Graphics.setSysUIPanelVisible(false);
  BlackHole3D.Graphics.setSysUIColorStyle(false);
  BlackHole3D.Graphics.setSysPanelUIDockArea(0);
  BlackHole3D.Camera.resetCamLocate();
  BlackHole3D.Camera.resetCamBound();
}
